package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendMailServlet extends HttpServlet {

	private static final long serialVersionUID = -9201649826567946642L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		SimpleEmail email = new SimpleEmail();
		email.setHostName("smtp.sina.com");
		email.setAuthentication("web08", "web2008");
		email.setCharset("UTF-8");
		try {
			email.setFrom(request.getParameter("from"));
			email.addTo(request.getParameter("to"));
			email.setSubject(request.getParameter("subject"));
			email.setMsg(request.getParameter("content"));
			email.send();
			request.setAttribute("sendmail.message", "邮件发送成功！");
		} catch (EmailException e) {
			e.printStackTrace();
			request.setAttribute("sendmail.message", "邮件发送不成功！");
		}
		request.getRequestDispatcher("/sendResult.jsp").forward(request,
				response);
	}

}
